//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    banners: [],  // banner
    recommands:[],  //推荐,
    promotionImg:'/images/title-promotion.png',
    brandImg:'/images/title-brand.png',
    creditImg:'/images/title-credit.png',
    promotionTops:[],
    promotionBottoms:[],
    sales:[],
    leases:[],
    flags:['正品保证','真机实拍','180天质保'],
    advers:[],
    cycleHeight:0,
    cycleWidth: 0,
    advertWidth:0,
    advertHeight:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    const rato = 360/750
    const advertRato = 150/680

    var that = this

    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          cycleHeight:res.screenWidth * rato,
          cycleWidth:res.screenWidth,
          advertWidth:res.screenWidth,
          advertHeight: res.screenWidth * advertRato
        })
      },
    })

    this.getBanner()
    this.getRecommand()
    this.getGalleries()
    this.getAdverList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },

  brandItemClick: function (e) {
    var index = Number(e.currentTarget.id)

    var obj = this.data.sales[index]

    wx.navigateTo({
      url: '../purchaseDetail/purchaseDetail?obj=' + JSON.stringify(obj),
    })
  },

  creditItemClick:function(e){
    var index = Number(e.currentTarget.id)

    var obj = this.data.leases[index]

    wx.navigateTo({
      url: '../leaseDetail/leaseDetail?obj='+JSON.stringify(obj),
    })
  },

  clickRecommand: function(e){
    var index = Number(e.currentTarget.id)
    console.log('click-->' + index)
    var obj = null

    for (var i in this.data.recommands){
        var subObj = this.data.recommands[i]

        if (subObj.id === index){
          obj = subObj
          break;
        }

    }

    console.log('obj' + JSON.stringify(obj))

    wx.navigateTo({
      // 
      url: '../daily/daily?obj='+JSON.stringify(obj),
    })
  },
  
  getBanner:function(){
    var that = this
    wx.request({
      url: app.net.baseURL + '/api/banner/queryBannerList',
      data: {},
      method: 'POST',
      header: app.net.header,
      success: function (res) {
        console.log(res.data.data)
        var images = new Array()

        for (var i = 0; i < res.data.data.length; i++) {
          var obj = res.data.data[i]
          images.push(obj.image)
        }

        that.setData({
          banners: res.data.data
        })
        console.log(images)
      }
    })
  },
  getRecommand:function(){
    var that = this
    wx.request({
      url: app.net.baseURL + '/api/recommend/queryRecommendList',
      data: {},
      method: 'POST',
      header: app.net.header,
      success: function (res) {
        console.log(res.data.data)
        var recommandList = new Array()
        for (var i = 0; i < res.data.data.length; i++){
          var obj = res.data.data[i]
          if (obj.image !== null){
            recommandList.push(obj)
          }
        }
        console.log(recommandList)
        that.setData({
          recommands: recommandList
        })
      }
    })
  },

// 获取橱窗数据
  getGalleries:function(){
    var that = this
    wx.request({
      url: app.net.baseURL + '/api/operation/findIndexInfo',
      data: {},
      method: 'POST',
      header: app.net.header,
      success: function (res) {
        console.log(res.data.data)

        var pros = res.data.data["10"]
        var tops = new Array()
        var bottoms = new Array()

        for (var i = 0; i < pros.length; i++){

          var obj = pros[i]

          if (i < 2) {
            tops.push(obj)
          }else{
            bottoms.push(obj)
          }
        }

        that.setData({
          promotionTops: tops,
          promotionBottoms: bottoms,
          sales: res.data.data["20"],
          leases: res.data.data["30"]
        })
      }
    })
  },
  // 获取广告
  getAdverList: function () {
    var that = this
    wx.request({
      url: app.net.baseURL + '/api/advert/queryAdverList',
      data: {},
      method: 'POST',
      header: app.net.header,
      success: function (res) {
        console.log(res.data.data)
        that.setData({
          advers: res.data.data
        })
      }
    })
  }
})